package com.supermap.dubbo.client.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.supermap.dubbo.service.GWFSDubboService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author xiaolu
 * @date 2018/6/11 下午5:19
 */
@RestController
@RequestMapping(value = "gwfs")
public class GWFSDataController {
    @Resource
    GWFSDubboService gwfsDubboService;

    @RequestMapping(value = "getRegions")
    @ResponseBody
    public JSONObject getRegions(@RequestBody Map mapParam) {
        JSONObject param = new JSONObject();

        param.put("userName","qxthjqxk");
        param.put("password","hjqxkyb");
        param.put("origin","manual");
        param.put("sequenceValue","08");
        param.put("forecastFileVersion","manual");

        param.put("forecastDate", mapParam.get("forecastDate"));
        param.put("forecastValidStart", mapParam.get("forecastValidStart"));
        param.put("forecastValidEnd", mapParam.get("forecastValidEnd"));
        param.put("interval", mapParam.get("interval"));
        param.put("element", mapParam.get("element"));
        param.put("coordinate", mapParam.get("coordinate"));
        JSONObject weatherData = gwfsDubboService.getWeatherPhenomenaInGridWithCoordinate(param);
        JSONObject elementData = gwfsDubboService.getGridDataWithCoordinate(param);
        if (weatherData.getInteger("code") == 404) {
            return weatherData;
        }
        if (elementData.getInteger("code") == 404) {
            return elementData;
        }
        weatherData = JSON.parseObject(weatherData.getString("data"));
        elementData = JSON.parseObject(elementData.getString("data"));
        JSONObject data = new JSONObject();
        data.put("weatherData", weatherData);
        data.put("elementData", elementData);
        return assistMsg(200, data);
    }

    /**
     * 错误信息
     *
     * @param errorCode 错误代码
     * @return 查询结果
     */
    private JSONObject assistMsg(int errorCode, Object data) {
        JSONObject msg = new JSONObject();
        msg.put("code", errorCode);
        if (errorCode == 404) {
            msg.put("msg", "SERVICE GET AN EXCEPTION");
        } else {
            msg.put("msg", "SERVICE GET SUCCESS");
        }
        msg.put("data", data);
        return msg;
    }
}
